package com.learning.assignment_1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.learning.database.DatabaseHelper;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.VideoView;

public class ListVideoActivity extends Fragment implements SurfaceHolder.Callback{
	SurfaceHolder surfaceholder;
	SurfaceView  surfaceview;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.list_videos_tab);
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
    	View mView = inflater.inflate(R.layout.list_videos_tab, container, false);
    	
    	final ListView lv = (ListView) mView.findViewById(R.id.videos_listview);
		//TODO
    	
    	final VideoView videoView = (VideoView) mView.findViewById(R.id.videoView);						
    	//Get available videos in sd card
		List<String> videoList = new ArrayList<String>();
		
	    File file = new File( "/sdcard" );       
	    File list[] = file.listFiles();

	    for( int i=0; i< list.length; i++)
	    {	
	    	if (list[i].getName().contains(".mp4")){
	    		videoList.add( list[i].getName());
	    	}
	    }
	    //////////////////////////////////
		
		
		lv.setAdapter(new VideoListAdapter(getActivity().getApplicationContext(), videoList));
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//Open video code here
				
				//videoView.setVideoPath("/sdcard/" + (String) parent.getItemAtPosition(position));	
				Uri video = Uri.parse("/sdcard/" + (String) parent.getItemAtPosition(position));
				MediaController mediaController = new MediaController(getActivity());
				mediaController.setAnchorView(videoView);
				
				videoView.setVideoURI(video);
				videoView.setMediaController(mediaController);
				videoView.start();
				//////////////////////
			}
		});
    	
    	
    	return mView;
    }
    
    class VideoListAdapter extends BaseAdapter{
		private List<String> mUsers;
		private LayoutInflater mInflater;
		public VideoListAdapter(Context ctx, List<String> lst){
			mUsers = lst;
			mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override 
		public int getCount(){
			return mUsers.size();
		}
		
		@Override
		public Object getItem(int i){
			return mUsers.get(i);
		}
		
		@Override
		public long getItemId(int item){
			return item;
		}
		
		@Override 
		public View getView(int position, View convertView, ViewGroup parent){
			View mView = convertView;
			if(mView == null){
				mView = mInflater.inflate(R.layout.videolist_item, parent, false);
			}
			TextView video = (TextView) mView.findViewById(R.id.listVideos);
			video.setText(mUsers.get(position));
			return mView;
			//hieu roi cam on dung co cuoi
		}
	}
    
    public void surfaceCreated(SurfaceHolder holder) {
    	
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    	
    }
}
